#include "aip8563_board.h"

#if _REAL_TIME_CLOCK_SOURCE_ == _EXTERNAL_CLOCK_SOURCE_

/**
 * @brief 配置 SDA 管脚
 *
 */
static void Aip8563SdaPinConfiguration(void);

/**
 * @brief 配置 SCL 管脚
 *
 */
static void Aip8563SclPinConfiguration(void);

/**
 * @brief 设置 SDA 管脚为高电平
 *
 */
static void Aip8563SetSdaHighLevel(void);

/**
 * @brief 设置 SDA 管脚为低电平
 *
 */
static void Aip5863SetSdaLowLevel(void);

/**
 * @brief 设置 SCL 管脚为高电平
 *
 */
static void Aip8563SetSclHighLevel(void);

/**
 * @brief 设置 SCL 管脚为低电平
 *
 */
static void Aip5863SetSclLowLevel(void);

/**
 * @brief 获取 SDA 数据线管脚电平状态
 *
 * @return bool - 管脚电平状态（true：高电平 false：低电平）
 */
static bool Aip8563ReadSdaPinLevel(void);

/**
 * @brief 将SDA设置为输入模式
 *
 */
static void Aip8563SetSdaInput(void);

/**
 * @brief 将SDA设置为输出模式
 *
 */
static void Aip8563SetSdaOutput(void);

static void Aip8563SdaPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V31_B_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);
#endif

  Aip8563SetSdaHighLevel();
}

static void Aip8563SclPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_ModeCfg(AIP8563_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_ModeCfg(AIP8563_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_ModeCfg(AIP8563_IIC_SCL_PIN, GPIO_ModeOut_PP_5mA);
#endif

  Aip8563SetSclHighLevel();
}

static void Aip8563SetSdaHighLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SDA_PIN);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_SetBits(AIP8563_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SDA_PIN);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_SetBits(AIP8563_IIC_SDA_PIN);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_SetBits(AIP8563_IIC_SDA_PIN);
#endif
}

static void Aip5863SetSdaLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SDA_PIN);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_ResetBits(AIP8563_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SDA_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SDA_PIN);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_ResetBits(AIP8563_IIC_SDA_PIN);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_ResetBits(AIP8563_IIC_SDA_PIN);
#endif
}

static void Aip8563SetSclHighLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SCL_PIN);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_SetBits(AIP8563_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(AIP8563_IIC_SCL_PIN);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_SetBits(AIP8563_IIC_SCL_PIN);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_SetBits(AIP8563_IIC_SCL_PIN);
#endif
}

static void Aip5863SetSclLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SCL_PIN);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_ResetBits(AIP8563_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SCL_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(AIP8563_IIC_SCL_PIN);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_ResetBits(AIP8563_IIC_SCL_PIN);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_ResetBits(AIP8563_IIC_SCL_PIN);
#endif
}

static bool Aip8563ReadSdaPinLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  if (GPIOB_ReadPortPin(AIP8563_IIC_SDA_PIN) == AIP8563_IIC_SDA_PIN)
    return true;

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  if (GPIOA_ReadPortPin(AIP8563_IIC_SDA_PIN) == AIP8563_IIC_SDA_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  if (GPIOB_ReadPortPin(AIP8563_IIC_SDA_PIN) == AIP8563_IIC_SDA_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  if (GPIOB_ReadPortPin(AIP8563_IIC_SDA_PIN) == AIP8563_IIC_SDA_PIN)
    return true;

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  if (GPIOB_ReadPortPin(AIP8563_IIC_SDA_PIN) == AIP8563_IIC_SDA_PIN)
    return true;

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  if (GPIOB_ReadPortPin(AIP8563_IIC_SDA_PIN) == AIP8563_IIC_SDA_PIN)
    return true;

#elif (_BOARD_TYPE_ == _CR01_BU_)
  if (GPIOB_ReadPortPin(AIP8563_IIC_SDA_PIN) == AIP8563_IIC_SDA_PIN)
    return true;
#endif

  return false;
}

static void Aip8563SetSdaInput(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeIN_PU);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeIN_PU);
#endif
}

static void Aip8563SetSdaOutput(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _V32_A_BOARD_)
  GPIOA_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _DA01_A_BOARD_)
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);

#elif (_BOARD_TYPE_ == _CR01_BU_)
  GPIOB_ModeCfg(AIP8563_IIC_SDA_PIN, GPIO_ModeOut_PP_5mA);
#endif
}

void Aip8563DelayUs(uint16_t Us)
{
  DelayUs(Us);
}

bool Aip8563GetSdaPinLevel(void)
{
  return Aip8563ReadSdaPinLevel();
}

void Aip8563SetSdaPinLevel(bool PinLevel)
{
  (PinLevel == true) ? Aip8563SetSdaHighLevel() : Aip5863SetSdaLowLevel();
}

void Aip8563SetSclPinLevel(bool PinLevel)
{
  (PinLevel == true) ? Aip8563SetSclHighLevel() : Aip5863SetSclLowLevel();
}

void Aip8563ConfigurationSdaInput(void)
{
  Aip8563SetSdaInput();
}

void Aip8563ConfigurationSdaOutput(void)
{
  Aip8563SetSdaOutput();
}

void Aip8563BoardInitialisation(void)
{
  Aip8563SdaPinConfiguration();
  Aip8563SclPinConfiguration();
}

#endif
